package com.c611.smartclassroom.controller;

import com.c611.smartclassroom.common.DataGridView;
import com.c611.smartclassroom.dto.SemeBySchidDto;
import com.c611.smartclassroom.model.Role;
import com.c611.smartclassroom.model.Sys;
import com.c611.smartclassroom.service.SysService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/*
* 系统设置: /sys	（李源）
	1,查询所有角色	queryAllRole()
	2，新增角色		addRole(角色对象)
	3，删除角色		delRole(角色ID)
	4，编辑权限		editAuth(授权码)
	5，设置每节课的时间	setCourseTime(第几节课，开始时间，结束时间)
	6，新增学期时间		setSemesterDate(学期ID，开始日期，结束日期)
	7，编辑学期时间		editSemesterDate(学期ID，开始日期，结束日期)
* */

@RestController
@RequestMapping("/sys")
public class SysController {

    @SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection")
    @Autowired
    SysService sysService;

    /**
     * 查询所有角色
     * @return
     */
    @RequestMapping("/queryAllRole")
    public DataGridView<Role> queryAllRole(){
        return new DataGridView<Role>(0,"ok",0L,sysService.queryAllRole());
    }

    /**
     * 增加角色
     * @param role
     * @return
     */
    @PostMapping("/addRole")
    public int addRole(@Param("role")Role role){
        //判空
        if (role == null) return 0;
        return sysService.addRole(role);
    }

    /*RoleMapper roleMapper;
    //管理员注册时验证用户名是否可用
    @RequestMapping(value="/xxx",method=RequestMethod.POST,consumes="application/json")
    @ResponseBody
    public boolean toVerifyUsername(@RequestBody Role role) {
        String username = role.getRoleid2();
        System.out.println(username);
        //根据用户名查询管理员(包括status为0的   以防恢复引起bug)
        RoleExample roleExample = new RoleExample();

        roleExample.createCriteria().andRoleid2EqualTo(username);

        List admin1 = roleMapper.selectByExample(roleExample);
        if (admin1 == null) {
            //返回true则为没有该用户名可以被注册
            return true;
        } else {
            return false;
        }

    }
*/
    /**
     *删除角色
     * @param roleid2
     * @return
     */
    @PostMapping("/delRole")
    public int delRole(@Param("roleid2") String roleid2){
        //判空
        if (roleid2 == null) return 0;
        return sysService.delRole(roleid2);
    }

    /**
     * 编辑授权码
     * @param roleid2
     * @param auth
     * @return
     */
    @PostMapping("/editAuth")
    public int editAuth(@Param("roleid2") String roleid2 , @Param("auth") String auth){
        //判空
        if (roleid2 == null) return 0;
        if (auth == null) return 0;
        return sysService.editAuth(roleid2,auth);
    }

    /**
     * 添加每节课的上下课时间
     * @param course 节次
     * @param stTime 上课时间
     * @param edTime 下课时间
     * @return
     */
    @PostMapping("/setCourseTime")
    public int setCourseTime(@Param("schoolId") String schoolId, @Param("semesterID") String semesterID, @Param("course") Integer course, @DateTimeFormat(pattern = "HH:mm") @Param("stTime")Date stTime, @DateTimeFormat(pattern = "HH:mm") @Param("edTime") Date edTime){
        //判空
//        System.out.println(schoolId);
//        System.out.println(semesterID);
        if (schoolId == null || "".equals(schoolId) || "null".equals(schoolId) || semesterID == null || "".equals(semesterID) || "null".equals(semesterID)) return 2;
        if (course <= 0 || course > 15 ) return 0;
        //如果上课时间大于下课时间
        if (stTime == null || edTime == null) return 0;
        if (stTime.after(edTime)) return 0;

        return sysService.setCourseTime(schoolId, semesterID, course, stTime, edTime);
    }

    /**
     * 删除学期时间的功能
     * @param schoolId
     * @param semesterID
     * @return
     */
    @PostMapping("/delSemesterDate")
    public int delSemesterDate(@Param("schoolId") String schoolId, @Param("semesterID") String semesterID){
        //判空
        if (schoolId == null || semesterID == null) return 0;
        return sysService.delSemesterDate(schoolId,semesterID);
    }

    /**
     * 添加学期时间
     */
    @PostMapping("/setSemesterDate")
    public int setSemesterDate(@Param("schoolId") String schoolId, @Param("semesterID") String semesterID, @DateTimeFormat(pattern = "yyyy-MM-dd") @Param("stTime") Date stTime , @DateTimeFormat(pattern = "yyyy-MM-dd") @Param("edTime") Date edTime){

        if (schoolId == null || semesterID == null) return 0;
        if (stTime == null || edTime == null) return 0;

        //如果开学时间大于放假时间
        if (stTime.after(edTime)) return 0;
        return sysService.setSemesterDate(schoolId, semesterID, stTime, edTime);

    }

    /**
     * 更改学期时间
     * @param schoolId 定位学校id
     * @param semesterID 再选择学期id
     * @param stTime 更改开学时间
     * @param edTime 更改放假时间
     * @return
     */
    @PostMapping("/editSemesterDate")
    public int editSemesterDate(@Param("schoolId") String schoolId, @Param("semesterID") String semesterID, @DateTimeFormat(pattern = "yyyy-MM-dd") @Param("stTime") Date stTime , @DateTimeFormat(pattern = "yyyy-MM-dd") @Param("edTime") Date edTime){
        //判空
        if (schoolId == null || semesterID == null || stTime == null || edTime == null) return 0;
        //当开学时间晚于放假时间则放回0
        if (stTime.after(edTime)) return 0;
        return sysService.editSemesterDate(schoolId, semesterID, stTime, edTime);
    }

    /**
     * 查询所有的学期，去除重复
     */
    @PostMapping("querySemesterIdDistinct")
    public List<String> querySemesterIdDistinct() {
        return sysService.querySemesterIdDistinct();
    }

    /**
     * 根据学期id和学校id来查询一天的时间
     */
    @PostMapping("/queryDayTime")
    public List<String> queryDayTime(@Param("schoolId") String schoolId, @Param("semesterID") String semesterID){
        List<String> timeValue = new ArrayList<>();
//        System.out.println(schoolId);
//        System.out.println(semesterID);
        if (schoolId == null || "null".equals(schoolId) || schoolId.isEmpty() || semesterID == null || "null".equals(semesterID) || semesterID.isEmpty()) {
            for(int i = 0;i < 10;i++) {
                timeValue.add("00:00 - 00:00");
            }
            return timeValue;
        }
        Sys sys = sysService.queryDayTime(schoolId,semesterID);
        if(sys == null) {
            for(int i = 0;i < 10;i++) {
                timeValue.add("00:00 - 00:00");
            }
            return timeValue;
        }

//        List<DayTimeDto> dayTimeDtos = new ArrayList<>();

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm:ss");
/*        if (sys.getC1start() == null) {
            timeValue.add("00:00 - 00:00");
        } else {
            timeValue.add(simpleDateFormat.format(sys.getC1start()).substring(0, 5) + " - " + simpleDateFormat.format(sys.getC1end()).substring(0, 5));
        }*/
        timeValue.add(simpleDateFormat.format(sys.getC1start()).substring(0, 5) + " - " + simpleDateFormat.format(sys.getC1end()).substring(0, 5));
        timeValue.add(simpleDateFormat.format(sys.getC2start()).substring(0,5) + " - " + simpleDateFormat.format(sys.getC2end()).substring(0,5));
        timeValue.add(simpleDateFormat.format(sys.getC3start()).substring(0,5) + " - " + simpleDateFormat.format(sys.getC3end()).substring(0,5));
        timeValue.add(simpleDateFormat.format(sys.getC4start()).substring(0,5) + " - " + simpleDateFormat.format(sys.getC4end()).substring(0,5));
        timeValue.add(simpleDateFormat.format(sys.getC5start()).substring(0,5) + " - " + simpleDateFormat.format(sys.getC5end()).substring(0,5));
        timeValue.add(simpleDateFormat.format(sys.getC6start()).substring(0,5) + " - " + simpleDateFormat.format(sys.getC6end()).substring(0,5));
        timeValue.add(simpleDateFormat.format(sys.getC7start()).substring(0,5) + " - " + simpleDateFormat.format(sys.getC7end()).substring(0,5));
        timeValue.add(simpleDateFormat.format(sys.getC8start()).substring(0,5) + " - " + simpleDateFormat.format(sys.getC8end()).substring(0,5));
        timeValue.add(simpleDateFormat.format(sys.getC9start()).substring(0,5) + " - " + simpleDateFormat.format(sys.getC9end()).substring(0,5));
        timeValue.add(simpleDateFormat.format(sys.getC10start()).substring(0,5) + " - " + simpleDateFormat.format(sys.getC10end()).substring(0,5));

        return timeValue;
    }

    /**
     * 根据学校id来查询所有的学期
     */
    @PostMapping("/querySemeBySchid")
    public List<SemeBySchidDto> querySemeBySchid(@Param("schoolId") String schoolId){
        if(schoolId == null) return null;
        List<Sys> list = sysService.querySemeBySchid(schoolId);

        List<SemeBySchidDto> dtos = new ArrayList<>();

        SimpleDateFormat format = new SimpleDateFormat("YYYY.MM.dd");
        for (Sys sys : list) {
            if(sys.getSesttime() != null && sys.getSeedtime() != null ) {
                dtos.add(new SemeBySchidDto(sys.getSemesterid(),format.format(sys.getSesttime()) + " - " + format.format(sys.getSeedtime())));
            }else {
                dtos.add(new SemeBySchidDto(sys.getSemesterid(),"-"));
            }
        }
        return dtos;
    }

}
